//找两个排序数组中位数
public class FindMedInSortedArr {
}
class FindMedInSortedArrSolution {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        if(nums1==null||nums2==null)
            return 0;
        if(nums1.length<1||nums2.length<1){
            if(nums1.length>0&&nums1.length%2==1)
                return nums1[nums1.length/2];
            else if(nums1.length>0)
                return ((double)nums1[nums1.length/2-1]+(double)nums1[nums1.length/2])/2;
            else if(nums2.length>0&&nums2.length%2==1)
                return nums2[nums2.length/2];
            else if(nums2.length>0)
                return ((double)nums2[nums2.length/2-1]+(double)nums2[nums2.length/2])/2;
            else
                return 0;
        }
        int s1=0;
        int s2=0;
        int e1=nums1.length-1;
        int e2=nums2.length-1;
        int mid1,mid2;
        while(e1-s1>1||e2-s2>1){
            mid1 = (s1+e1)/2;
            mid2 = (s2+e2)/2;
            if(nums1[mid1]==nums2[mid2])
                return nums1[mid1];
            else if(nums1[mid1]>nums2[mid2]){
                e1 = mid1;
                s2 = mid2;
            }
            else{
                s1 = mid1;
                e2 = mid2;
            }
        }
        if(e1-s1==1&&e2-s2==1){
            if(nums1[e1]<=nums2[s2])
                return ((double)nums1[e1]+(double)nums2[s2])/2;
            else if(nums1[s1]>nums2[e2])
                return ((double)nums1[s1]+(double)nums2[e2])/2;
            else if(nums1[e1]<nums2[e2])
                return ((double)nums1[e1]+(double)nums2[s2])/2;
            else
                return ((double)nums1[e2]+(double)nums2[s1])/2;
        }
        else if(e1-s1==0){
            if(nums2[e2]<nums1[s1])
                return nums2[e2];
            else if(nums2[s2]>nums1[s1])
                return nums2[s2];
            else
                return nums1[s1];
        }
        else{
            if(nums1[e1]<nums2[s2])
                return nums1[e1];
            else if(nums1[s1]>nums2[s2])
                return nums1[s1];
            else
                return nums2[s2];
        }
    }
}